<?php

namespace App\Models\SystemManagement;

use App\Models\CommonModel;

class Privilege extends CommonModel
{
    protected $table = 'privileges';
    public $primaryKey = 'id';

    protected $hidden = ['updated_at','created_at','deleted_at'];
    protected $guarded = ['privilege_ids'];

    /**
     * @param $data 表单提交数据
     * @return string 返回响应结果
     * 添加权限数据
     */
    public static function addData($data){
        //保存数据
        $param = new self();
        /*$params->name = $data['name'];
        $params->group_name = $data['groupName'];
        $params->description = $data['description'];
        $params->status = $data['status'];
        $params->save*/
        $res = $param->create($data);
        return $res;
    }

    /**
     * @return mixed
     * 获取权限 带分页
     */
    public static function getPageData($request){
        $per_page = $request->input('per_page',10);
        return self::orderBy('sort', 'desc')
                    ->with('files')
                    ->where(function($query) use ($request){
                        $key = $request->input('key', '');
                        $menu = $request->input('menu', '');
                        $status = $request->input('status', '');
                        $parent_id = $request->input('parent_id', '');
                        if($key){
                            $query->where('name', 'like', "%$key%");//权限名称
                            $query->orWhere('route', 'like', "%$key%");//权限路由
                        }
                        if(is_numeric($menu)){
                            $query->where('menu', $menu);
                        }
                        if(is_numeric($status)){
                            $query->where('status', $status);
                        }
                        if(is_numeric($parent_id)){
                            $query->where('parent_id', $parent_id);
                        }
                    })
                    ->paginate($per_page);
    }

    /**
     * @return mixed
     * 获取权限
     */
    public static function getAll($request)
    {
        return self::orderBy('sort', 'desc')
                        ->with('files')
                        ->where(function($query) use ($request){
                            $key = $request->input('key', '');
                            $menu = $request->input('menu', '');
                            $status = $request->input('status', '');
                            if($key){
                                $query->where('name', 'like', "%$key%");//权限名称
                                $query->orWhere('route', 'like', "%$key%");//权限路由
                            }
                            if(is_numeric($menu)){
                                $query->where('menu', $menu);
                            }
                            if(is_numeric($status)){
                                $query->where('status', $status);
                            }
                        })
                        ->get();
    }

    /**
     * @param $id
     * @return mixed
     * 获取单条权限数据
     */
    public static function getOne($id){
        $query = self::find($id);
        return $query;
    }

    /**
     * @param $name
     * @return mixed
     * 通过条件查找
     */
    public static function getByCondition($con){
        $query = self::getTableDataByCondition($con,new self());
        return $query;
    }

    /**
     * @param $arr
     * @return bool
     * 修改某条数据
     */
    public static function updateData($arr,$id){
        $query = self::updateTableData($arr,$id,new self());
        return $query;
    }

    /**
     * @param $ids
     * @return \App\Models\Error
     * 删除
     */
    static function del($ids){
        $query = self::batchDelTableData($ids,new self());
        return $query;
    }

    /**
     * @param $ids
     * @return \App\Models\Error
     * 恢复
     */
    static function recover($ids){
        $query = self::batchResetTableData($ids,new self());
        return $query;
    }

    /**
     * 转成 菜单的格式
     * @param $list
     * @param $pid
     * @return array
     * [{
     *   "id": 18,
     *   "pid": 0,
     *   "name": "functions",
     *   "label": "功能管理",
     *   "menu": 1,
     *   "icon": "el-icon-setting",
     *   "children": []
     * }]
     */
    public static function getMenusTree($list, $pid)
    {
        $tree = [];
        foreach ($list as $row) {
            if($row['parent_id'] === $pid) {
                $temp = [
                    'id' => $row['id'],
                    'parent_id' => $row['parent_id'],
                    'name' => $row['name'],
                    'route' => '/'.trim($row['route'], '/'),
                    'menu' => $row['menu'],
                    'icon' => $row['icon'],
                ];
                $children = self::getMenusTree($list, $row['id']);
                if(!empty($children)) {
                    $temp['children'] = $children;
                }
                $tree[] = $temp;
            }
        }
        return $tree;
    }

    function files()
    {
        return $this->hasMany(PrivilegeFile::class,'privilege_id')->with('file');
    }

}
